import { isAuthenticated } from "@/lib/auth";
import { NextRequest, NextResponse } from "next/server";
import { GoNextMiddleware } from "./util/chain";

export async function authMiddleware(
  request: NextRequest,
  next: GoNextMiddleware
) {
  const { pathname } = request.nextUrl;
  if (pathname.startsWith("/admin")) {
    const flag = await isAuthenticated(request);
    if (!flag) {
      return NextResponse.redirect(new URL("/login", request.url));
    }
    // const user: any = flag.jwtData;
    // request.cookies.set('userId', user.id)
  }
  return next();

  // return NextResponse.next();
}
